package com.sun.protocol;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author 烛龙神
 * @Description 数据协议层
 * @date 2021-04-07 22:09
 */
public class Protocol {

    public static final String X = "*";
    public static final String S = "$";
    public static final String K = "\r\n";

    public static void sendCommand(Protocol.Command command,OutputStream outputStream,byte[] ...b)
    {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(X).append(b.length+1).append(K); //*3
        stringBuffer.append(S).append(command.name().length()).append(K); //$3
        stringBuffer.append(command).append(K);//SET
        for (byte []bytes:b)
        {
            stringBuffer.append(S).append(bytes.length).append(K);//$6 $4
            stringBuffer.append(new String(bytes)).append(K);//$4 $6
        }
        try
        {
           // System.out.println(stringBuffer.toString());
            outputStream.write(stringBuffer.toString().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static enum Command {
        SET,GET
    }
}
